﻿/*****************************************介绍*****************************************
 * 作者：Ywh
 * 创建时间：2024-02-28　04:42:20
 * 功能：自动设置预制体
**************************************************************************************/

using System;
using UnityEditor;
using UnityEngine;

public class AutoSetPrefabs : AssetPostprocessor
{
    private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        for (int i = 0; i < importedAssets.Length; i++)
        {
            switch (importedAssets[i])
            {
                case "Assets/HotFixResources/Terrain/SceneCapture/CreateRole/Human/Human3D.prefab":
                case "Assets/HotFixResources/Terrain/SceneCapture/CreateRole/Orc/Orc3D.prefab":
                    SetCreateCharacterPrefab(importedAssets[i]);
                    break;
                case "Assets/HotFixResources/Terrain/Environment/SelectRole/Terrain.prefab":
                    SetSelectCharacterPrefab(importedAssets[i]);
                    break;
            }
        }
    }

    private static void SetCreateCharacterPrefab(string path)
    {
        string posRootPath = "Assets/HotFixResources/UI/Panels/CreateCharacterPanel/Prefabs/";
        if (path.StartsWith("Assets/HotFixResources/Terrain/SceneCapture/CreateRole/Human"))
            ChangePrefab(path, human => SetCreateCharacterPrefab(human, $"{posRootPath}HumanPos/AllPos.prefab"));
        else if (path.StartsWith("Assets/HotFixResources/Terrain/SceneCapture/CreateRole/Orc"))
            ChangePrefab(path, orc => SetCreateCharacterPrefab(orc, $"{posRootPath}OrcPos/AllPos.prefab"));
    }

    private static void SetCreateCharacterPrefab(GameObject prefabInst, string posPath)
    {
        if (prefabInst != null)
        {
            var cams = prefabInst.transform.GetComponentsInChildren<Camera>();
            foreach (var cam in cams)
            {
                if (cam.gameObject.activeSelf)
                {
                    cam.gameObject.SetActive(false);
                }
            }

            var allPos = prefabInst.transform.Find("AllPos");
            if (allPos == null)
            {
                var obj = PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath<GameObject>(posPath)) as GameObject;
                obj.transform.SetParent(prefabInst.transform);
                obj.transform.SetAsLastSibling();
                PrefabUtility.UnpackPrefabInstance(obj, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
            }
        }
    }

    private static void SetSelectCharacterPrefab(string path)
    {
        ChangePrefab(path, terrain =>
        {
            var cams = terrain.transform.GetComponentsInChildren<Camera>();
            foreach (var cam in cams)
            {
                if (cam.gameObject.activeSelf)
                    cam.gameObject.SetActive(false);
            }

            var birch_stump_old_cut_prefab = terrain.transform.Find("Birch_stump_old_cut_prefab");
            var mashroom_2 = terrain.transform.Find("Mashroom_2");
            if (birch_stump_old_cut_prefab != null) birch_stump_old_cut_prefab.gameObject.SetActive(false);
            if (mashroom_2 != null) mashroom_2.gameObject.SetActive(false);

            var model = terrain.transform.Find("model");
            if (model != null)
                model.transform.SetPositionAndRotation(new Vector3(21.9510002f, 12.3529997f, 27.6019993f), Quaternion.Euler(new Vector3(62.02174f, 324.42746f, 235.724136f)));
        });
    }

    private static void ChangePrefab(string path, Action<GameObject> todo)
    {
        var prefabObj = PrefabUtility.LoadPrefabContents(path);
        todo?.Invoke(prefabObj);
        PrefabUtility.SaveAsPrefabAsset(prefabObj, path);
        PrefabUtility.UnloadPrefabContents(prefabObj);
    }
}